﻿namespace SuperMarieApi.App.Controllers.Framework.DataAccess.SqlBuilder
{
    using System.Data;

    public class CustomSqlCondition:Condition
    {
        public SqlRelation Relation { get; private set; }

        public string CustomSql { get; private set; }

        public SqlOperator Operator { get; private set; }

        public string ParameterName { get; private set; }

        public DbType DbType { get; set; }

        public object Value { get; private set; }

        public CustomSqlCondition(SqlRelation relation, string customSql)
            :this(relation,customSql,null, DbType.AnsiString,null)
        {
        }

        public CustomSqlCondition(SqlRelation relation,string customSql, string paramName, DbType dbType, object value)
        {
            this.Relation = relation;
            this.CustomSql = customSql;
            this.ParameterName = paramName;
            this.DbType = dbType;
            this.Value = value;
        }

        public override string GetPrepend()
        {
            return this.GetRelationString(this.Relation);
        }

        public override string Build2String(DataCommand dbCommand)
        {
            if (!string.IsNullOrEmpty(this.ParameterName))
            {
                dbCommand.AddParameter(this.ParameterName, this.DbType, 0, ParameterDirection.Input, this.Value);
            }
            return this.CustomSql;
        }
    }
}
